﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using appDataContracts;
using appClient;
using appInterfaces;
using System.ServiceModel;

namespace appClient.Account
{
	public partial class Register : System.Web.UI.Page
	{

		IClient client;

		protected void Page_Load(object sender, EventArgs e)
		{
			//RegisterUser.ContinueDestinationPageUrl = Request.QueryString["ReturnUrl"];
			client = new ChannelFactory<IClient>("clientHttp").CreateChannel();
			if (Session["id"] != null)
				Response.Redirect("../Default.aspx");
		}

		protected void RegisterUser_CreatedUser(object sender, EventArgs e)
		{
			//FormsAuthentication.SetAuthCookie(RegisterUser.UserName, false /* createPersistentCookie */);

			/*string continueUrl = RegisterUser.ContinueDestinationPageUrl;
			if (String.IsNullOrEmpty(continueUrl))
			{
				continueUrl = "~/";
			}
			Response.Redirect(continueUrl);*/

			/*bool registerSuccessful = client.registerAccount(UserName.Text, LastName.Text, Address.Text, Zip.Text, City.Text, Email.Text, Phone.Text, Password.Text);

			if (registerSuccessful)
				Response.Redirect("../Default.aspx");*/
		}

		protected void CreateUserButton_Click(object sender, EventArgs e)
		{
			bool registerSuccessful = client.registerAccount(new SerializedClient(
				0, 
				UserName.Text, 
				LastName.Text, 
				Address.Text, 
				Zip.Text, 
				City.Text, 
				Phone.Text, 
				Email.Text, 
				Country.Text, 
				Password.Text));

			if (registerSuccessful)
			{
				// check if user exists
				SerializedClient sCli = client.checkAccount(Email.Text.ToString(), Password.Text.ToString());
				
				List<CaddieProduct> caddie = new List<CaddieProduct>();

				// creating a new session
				Session["user"] = sCli;
				Session["caddie"] = caddie;
				Session.Timeout = 600;

				Response.Redirect("../Default.aspx");
			}
		}

	}
}
